package com.aaa.stock.controller.api;

import com.aaa.entity.StockMedicines;
import com.aaa.entity.StockProducer;
import com.aaa.stock.service.MedicinesService;
import com.aaa.stock.vo.MedicinesrVo;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/stock/api/Medicinesr")
public class ApiMedicinesrControlle {
    @Autowired
    private MedicinesService medicinesService;
    //分页查询
    @PostMapping("/list1/{currentPage}/{pageSize}")
    public Result<IPage<StockMedicines>> list1(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody MedicinesrVo medicinesrVo
    ) {
        System.out.println("=== === " + currentPage);
        System.out.println("=== === " + pageSize);
        System.out.println("medicinesrVo = " + medicinesrVo);
//
        return medicinesService.getByPage(currentPage, pageSize, medicinesrVo);
    }
    //查询全部
    @PostMapping("/list")
    public Result<List<StockProducer>> list(){

        return medicinesService.getAll();
    }


    //添加
    @PostMapping("addstu")
    public Result addstu(@RequestBody StockMedicines stockMedicines){
        if (Objects.nonNull(stockMedicines.getMedicinesId())){
            stockMedicines.setUpdateTime(LocalDateTime.now());
        }else {
            stockMedicines.setCreateTime(LocalDateTime.now());
        }
         medicinesService.saveOrUpdate(stockMedicines);
        return new Result (2000,"成功");
    }


    //删除
    @DeleteMapping("/{medicinesId}")
    public Result delStu( @PathVariable Integer medicinesId){
        medicinesService.removeById(medicinesId);
        return new Result(2000,"成功");
    }

    //批量删除
    @PostMapping("/del1")
    public Result delStu1( @RequestBody List<StockMedicines> stockMedicines){
        stockMedicines.forEach((StockMedicines stockMedicines1)->{
            boolean b = medicinesService.removeById(stockMedicines1.getMedicinesId());
        });
        return new Result(2000,"成功");
    }






}
